package com.primb.androidlibs.utils;

import android.graphics.Color;
import android.graphics.Matrix;
import android.support.v4.content.ContextCompat;

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.BubbleChart;
import com.github.mikephil.charting.charts.CombinedChart;
import com.github.mikephil.charting.charts.HorizontalBarChart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.charts.RadarChart;
import com.github.mikephil.charting.charts.ScatterChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.BubbleData;
import com.github.mikephil.charting.data.BubbleDataSet;
import com.github.mikephil.charting.data.BubbleEntry;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.data.RadarData;
import com.github.mikephil.charting.data.RadarDataSet;
import com.github.mikephil.charting.data.RadarEntry;
import com.github.mikephil.charting.data.ScatterData;
import com.github.mikephil.charting.data.ScatterDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.interfaces.datasets.IBubbleDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.interfaces.datasets.IRadarDataSet;
import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet;
import com.github.mikephil.charting.utils.ViewPortHandler;
import com.primb.androidlibs.R;
import com.primb.androidlibs.override.ChartValueFormatter;
import com.primb.androidlibs.override.MPChartMarkerView;
import com.primb.androidlibs.override.StringAxisValueFormatter;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by Chen on 2016/12/28.
 * 功能描述：图表帮助类
 */

public class MPChartHelper {
    public static final int[] PIE_COLORS = {
            Color.parseColor("#6a8fe2"), Color.parseColor("#c480d2"), Color.parseColor("#ff83b0"),
            Color.parseColor("#ffa8a4"), Color.parseColor("#ffd9a3"), Color.parseColor("#ffe68f"),
            Color.parseColor("#98e994"), Color.parseColor("#7ed8f7"), Color.parseColor("#7eb0fa"),
            Color.parseColor("#be8bc8"), Color.parseColor("#f889b2"), Color.parseColor("#7a96d3")
    };

    public static final int[] LINE_COLORS = {
            Color.rgb(140, 210, 118), Color.rgb(159, 143, 186), Color.rgb(233, 197, 23)
    };//绿色，紫色，黄色

    public static final int[] BAR_COLORS = {
            Color.rgb(140, 210, 118), Color.rgb(159, 143, 186), Color.rgb(233, 197, 23)
    };//绿色，紫色，黄色

    public static final int[] LINE_FILL_COLORS = {
            Color.rgb(222, 239, 228), Color.rgb(246, 234, 208), Color.rgb(235, 228, 248)
    };

    public static final float X_TEXT_SIZE = 12;

    public static final float LEGEND_TEXT_SIZE = 12;

    public static final float LEGEND_SIZE = 6;

    public static final float VALUE_SIZE = 8;

    /**
     * 单数据集。设置柱状图样式，X轴为字符串，Y轴为数值
     *
     * @param title 图例文字
     */
    public static void setBarChart(BarChart barChart, List<String> xAxisValue, List<Float> yAxisValue, String title, Integer barColor) {
        barChart.getDescription().setEnabled(false);//设置描述
        barChart.setPinchZoom(false);//设置按比例放缩柱状图
        barChart.setScaleEnabled(false);

        //设置自定义的markerView
//        MPChartMarkerView markerView = new MPChartMarkerView(barChart.getContext(), R.layout.custom_marker_view);
//        barChart.setMarker(markerView);

        Matrix mMatrix = new Matrix();
        mMatrix.postScale((xAxisValue.size() > 6 ? (float) (Math.ceil(xAxisValue.size() / 6) * (xAxisValue.size() > 20 ? 1.3f : 1f)) : 0f), 0f);
        barChart.getViewPortHandler().refresh(mMatrix, barChart, true);

        //x坐标轴设置
        IAxisValueFormatter xAxisFormatter = new StringAxisValueFormatter(xAxisValue);//设置自定义的x轴值格式化器
        XAxis xAxis = barChart.getXAxis();//获取x轴
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);//设置X轴标签显示位置
        xAxis.setDrawGridLines(false);//不绘制格网线
        xAxis.setGranularity(1f);//设置最小间隔，防止当放大时，出现重复标签。
        xAxis.setValueFormatter(xAxisFormatter);
        xAxis.setTextSize(X_TEXT_SIZE - 3);//设置标签字体大小
        xAxis.setLabelCount(xAxisValue.size());//设置标签显示的个数

        //y轴设置
        YAxis leftAxis = barChart.getAxisLeft();//获取左侧y轴
        leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);//设置y轴标签显示在外侧
        leftAxis.setAxisMinimum(0f);//设置Y轴最小值
        leftAxis.setDrawGridLines(false);
        leftAxis.setDrawLabels(true);//禁止绘制y轴标签
        leftAxis.setDrawAxisLine(true);//禁止绘制y轴

        barChart.getAxisRight().setEnabled(false);//禁用右侧y轴

        //图例设置
        Legend legend = barChart.getLegend();
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);//图例水平居中
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);//图例在图表上方
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);//图例的方向为水平
        legend.setDrawInside(false);//绘制在chart的外侧
        legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);//图例中的文字方向

        legend.setForm(Legend.LegendForm.SQUARE);//图例窗体的形状
        legend.setFormSize(LEGEND_SIZE);//图例窗体的大小
        legend.setTextSize(LEGEND_TEXT_SIZE);//图例文字的大小
        //legend.setYOffset(-2f);

        //设置柱状图数据
        setBarChartData(barChart, yAxisValue, title, barColor);

        barChart.setExtraBottomOffset(10);//距视图窗口底部的偏移，类似与paddingbottom
        barChart.setExtraTopOffset(0);//距视图窗口顶部的偏移，类似与paddingtop
        barChart.setFitBars(true);//使两侧的柱图完全显示
        barChart.animateY(500);//数据显示动画，从左往右依次显示
    }

    /**
     * 设置柱图
     */
    private static void setBarChartData(BarChart barChart, List<Float> yAxisValue, String title, Integer barColor) {

        ArrayList<BarEntry> entries = new ArrayList<>();

        for (int i = 0, n = yAxisValue.size(); i < n; ++i) {
            entries.add(new BarEntry(i, yAxisValue.get(i)));
        }

        BarDataSet set1;

        if (barChart.getData() != null && barChart.getData().getDataSetCount() > 0) {
            set1 = (BarDataSet) barChart.getData().getDataSetByIndex(0);
            set1.setValues(entries);
            set1.setLabel(title);
//            set1.setHighlightEnabled(false);
            barChart.getData().notifyDataChanged();
            barChart.notifyDataSetChanged();
        } else {
            set1 = new BarDataSet(entries, title);
            if (barColor == null) {
                //设置set1的柱的默认颜色
                set1.setColor(ContextCompat.getColor(barChart.getContext(), R.color.colorPrimaryDark));
            } else {
                set1.setColor(barColor);
            }
            ArrayList<IBarDataSet> dataSets = new ArrayList<>();
            dataSets.add(set1);

            BarData data = new BarData(dataSets);
            data.setValueTextSize(8f);
            data.setBarWidth(0.7f);
            data.setValueFormatter(new ChartValueFormatter());
            barChart.setData(data);
        }
    }


    /**
     * 设置正负值在0轴上下方的柱图
     *
     * @param barChart
     * @param xAxisValue x轴的值。必须与yAxisValue的值个数相同
     * @param yAxisValue y轴的值。必须与xAxisValue的值个数相同
     * @param title
     */
    public static void setPositiveNegativeBarChart(BarChart barChart, List<String> xAxisValue, List<Float> yAxisValue, String title, int[] mColors) {
        barChart.setDrawBarShadow(false);
        barChart.setDrawValueAboveBar(true);
        barChart.getDescription().setEnabled(false);
        // scaling can now only be done on x- and y-axis separately
        barChart.setPinchZoom(false);
        barChart.setDrawGridBackground(false);
        barChart.setExtraBottomOffset(10);
        barChart.setExtraTopOffset(30);
        barChart.setHighlightFullBarEnabled(false);

//        MPChartMarkerView markerView = new MPChartMarkerView(barChart.getContext(), R.layout.custom_marker_view);
//        barChart.setMarker(markerView);

        Matrix mMatrix = new Matrix();
        mMatrix.postScale((xAxisValue.size() > 6 ? (float) (Math.ceil(xAxisValue.size() / 6) * (xAxisValue.size() > 20 ? 1.3f : 1f)) : 0f), 0f);
        barChart.getViewPortHandler().refresh(mMatrix, barChart, true);

        XAxis xAxis = barChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        xAxis.setDrawAxisLine(false);
        xAxis.setTextColor(Color.DKGRAY);
        xAxis.setTextSize(X_TEXT_SIZE - 3);
        xAxis.setAxisMinimum(0f);
        xAxis.setAxisMaximum(xAxisValue.size());
        xAxis.setLabelCount(xAxisValue.size());
        xAxis.setCenterAxisLabels(true);
        xAxis.setGranularity(1f);

        YAxis left = barChart.getAxisLeft();
        left.setDrawLabels(true);
        left.setSpaceTop(25f);
        left.setSpaceBottom(25f);
        left.setDrawAxisLine(true);
        left.setDrawGridLines(false);
        left.setDrawZeroLine(true); // draw a zero line
        left.setZeroLineColor(Color.DKGRAY);
        left.setZeroLineWidth(1f);
        left.setEnabled(true);
        barChart.getAxisRight().setEnabled(false);

        //图例设置
        Legend legend = barChart.getLegend();
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        legend.setDrawInside(false);
        legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);
        legend.setForm(Legend.LegendForm.SQUARE);
        legend.setTextSize(LEGEND_TEXT_SIZE);


        // THIS IS THE ORIGINAL DATA YOU WANT TO PLOT
        final List<Data> data = new ArrayList<>();
        for (int i = 0, n = xAxisValue.size(); i < n; ++i) {
            data.add(new Data(0.5f + i, yAxisValue.get(i), xAxisValue.get(i)));
        }

        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return data.get(Math.min(Math.max((int) value, 0), data.size() - 1)).xAxisValue;
            }
        });

        setData(barChart, data, title, mColors);
    }

    private static void setData(BarChart barChart, List<Data> dataList, String title, int[] mColors) {

        ArrayList<BarEntry> values = new ArrayList<BarEntry>();
        List<Integer> colors = new ArrayList<Integer>();

        int green = Color.rgb(195, 221, 155);
        int red = Color.rgb(237, 189, 189);

        for (int i = 0; i < dataList.size(); i++) {

            Data d = dataList.get(i);
            BarEntry entry = new BarEntry(d.xValue, d.yValue);
            values.add(entry);

            // specific colors
//            if (d.yValue >= 0)
            colors.add(mColors[0]);
//            else
//                colors.add(green);
        }

        BarDataSet set;

        if (barChart.getData() != null &&
                barChart.getData().getDataSetCount() > 0) {
            set = (BarDataSet) barChart.getData().getDataSetByIndex(0);
            set.setValues(values);
            barChart.getData().notifyDataChanged();
            barChart.notifyDataSetChanged();
        } else {
            set = new BarDataSet(values, title);
            set.setColors(colors);
//            set.setValueTextColors(colors);

            BarData data = new BarData(set);
            data.setValueTextSize(VALUE_SIZE);
            data.setValueFormatter(new PositiveNegativeBarChartValueFormatter());
            data.setBarWidth(0.4f);

            barChart.setData(data);
            barChart.invalidate();
        }
    }

    /**
     * 设置正负值在0轴上下方的柱图
     */
    public static void setPositiveNegativesBarChart(BarChart barChart, final List<String> xAxisValue, List<Float> yAxisValue1, List<Float> yAxisValue2, String bartilte1, String bartitle2, int[] mColors) {
        barChart.setDrawBarShadow(false);
        barChart.setDrawValueAboveBar(true);
        barChart.getDescription().setEnabled(false);
        // scaling can now only be done on x- and y-axis separately
        barChart.setPinchZoom(false);
        barChart.setDrawGridBackground(false);
        barChart.setExtraBottomOffset(10);
        barChart.setExtraTopOffset(30);
        barChart.setHighlightFullBarEnabled(false);

//        MPChartMarkerView markerView = new MPChartMarkerView(barChart.getContext(), R.layout.custom_marker_view);
//        barChart.setMarker(markerView);

        Matrix mMatrix = new Matrix();
        mMatrix.postScale((xAxisValue.size() > 6 ? (float) (Math.ceil(xAxisValue.size() / 6) * (xAxisValue.size() > 20 ? 1.3f : 1f)) : 0f), 0f);
        barChart.getViewPortHandler().refresh(mMatrix, barChart, true);

        XAxis xAxis = barChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        xAxis.setDrawAxisLine(false);
        xAxis.setTextColor(Color.DKGRAY);
        xAxis.setTextSize(X_TEXT_SIZE - 3);
        xAxis.setAxisMinimum(0f);
        xAxis.setAxisMaximum(xAxisValue.size());
        xAxis.setLabelCount(xAxisValue.size());
        xAxis.setCenterAxisLabels(true);
        xAxis.setGranularity(1f);

        YAxis left = barChart.getAxisLeft();
        left.setDrawLabels(true);
        left.setSpaceTop(25f);
        left.setSpaceBottom(25f);
        left.setDrawAxisLine(true);
        left.setDrawGridLines(false);
        left.setDrawZeroLine(true); // draw a zero line
        left.setZeroLineColor(Color.DKGRAY);
        left.setZeroLineWidth(1f);
        left.setEnabled(true);
        barChart.getAxisRight().setEnabled(false);

        //图例设置
        Legend legend = barChart.getLegend();
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        legend.setDrawInside(false);
        legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);
        legend.setForm(Legend.LegendForm.SQUARE);
        legend.setTextSize(LEGEND_TEXT_SIZE);

        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                if (value < 0) {
                    return "";
                } else {
                    return xAxisValue.get(((int) value) % xAxisValue.size());
                }
            }
        });

        setPositiveNegativesBarData(barChart, xAxisValue, yAxisValue1, yAxisValue2, bartilte1, bartitle2, mColors);
    }

    /**
     * 设置柱状图数据源
     */
    private static void setPositiveNegativesBarData(BarChart barChart, List<String> xAxisValue, List<Float> yAxisValue1, List<Float> yAxisValue2, String bartilte1, String bartitle2, int[] mColors) {
        float groupSpace = 0.2f;
        float barSpace = 0.05f;
        float barWidth = 0.35f;
        // (0.45 + 0.03) * 2 + 0.04 = 1，即一个间隔为一组，包含两个柱图 -> interval per "group"

        ArrayList<BarEntry> entries1 = new ArrayList<>();
        ArrayList<BarEntry> entries2 = new ArrayList<>();

        for (int i = 0, n = yAxisValue1.size(); i < n; ++i) {
            entries1.add(new BarEntry(i, yAxisValue1.get(i)));
            entries2.add(new BarEntry(i, yAxisValue2.get(i)));
        }

        BarDataSet dataset1, dataset2;

        if (barChart.getData() != null && barChart.getData().getDataSetCount() > 0) {
            dataset1 = (BarDataSet) barChart.getData().getDataSetByIndex(0);
            dataset2 = (BarDataSet) barChart.getData().getDataSetByIndex(1);
            dataset1.setValues(entries1);
            dataset2.setValues(entries2);
            barChart.getData().notifyDataChanged();
            barChart.notifyDataSetChanged();
        } else {
            dataset1 = new BarDataSet(entries1, bartilte1);
            dataset2 = new BarDataSet(entries2, bartitle2);

            dataset1.setColor(mColors[0]);
            dataset2.setColor(mColors[1]);

            ArrayList<IBarDataSet> dataSets = new ArrayList<>();
            dataSets.add(dataset1);
            dataSets.add(dataset2);

            BarData data = new BarData(dataSets);
            data.setValueTextSize(VALUE_SIZE);
//            data.setBarWidth(0.9f);
            data.setBarWidth(0.3f);
            data.setValueFormatter(new IValueFormatter() {
                @Override
                public String getFormattedValue(float value, Entry entry, int i, ViewPortHandler viewPortHandler) {
                    return FormatUtils.format(value, 2) + "";
                }
            });

            barChart.setData(data);
        }

        barChart.getBarData().setBarWidth(barWidth);
        barChart.getXAxis().setAxisMinimum(0);
        // barData.getGroupWith(...) is a helper that calculates the width each group needs based on the provided parameters
        barChart.getXAxis().setAxisMaximum(barChart.getBarData().getGroupWidth(groupSpace, barSpace) * xAxisValue.size() + 0);
        barChart.groupBars(0, groupSpace, barSpace);
    }

    /**
     * positive-negative data model representing data.
     */
    private static class Data {

        public String xAxisValue;
        public float yValue;
        public float xValue;

        public Data(float xValue, float yValue, String xAxisValue) {
            this.xAxisValue = xAxisValue;
            this.yValue = yValue;
            this.xValue = xValue;
        }
    }

    private static class PositiveNegativeBarChartValueFormatter implements IValueFormatter {

        private DecimalFormat mFormattedStringCache;

        public PositiveNegativeBarChartValueFormatter() {
            mFormattedStringCache = new DecimalFormat("######.00");
        }

        @Override
        public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
            return mFormattedStringCache.format(value);
        }
    }


    /**
     * 设置双柱状图样式
     */
    public static void setTwoBarChart(BarChart barChart, final List<String> xAxisValue, List<Float> yAxisValue1, List<Float> yAxisValue2, String bartilte1, String bartitle2, int[] mColors) {
        barChart.getDescription().setEnabled(false);//设置描述
        barChart.setPinchZoom(true);//设置按比例放缩柱状图
        barChart.setExtraBottomOffset(10);

        MPChartMarkerView markerView = new MPChartMarkerView(barChart.getContext(), R.layout.custom_marker_view);
        barChart.setMarker(markerView);

        Matrix mMatrix = new Matrix();
        mMatrix.postScale((xAxisValue.size() > 6 ? (float) (Math.ceil(xAxisValue.size() / 6) * (xAxisValue.size() > 20 ? 1.3f : 1f)) : 0f), 0f);
        barChart.getViewPortHandler().refresh(mMatrix, barChart, true);

        //x坐标轴设置
        XAxis xAxis = barChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        xAxis.setGranularity(1f);
        xAxis.setTextSize(X_TEXT_SIZE - 3);
        xAxis.setLabelCount(xAxisValue.size());
        xAxis.setCenterAxisLabels(true);//设置标签居中
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float v, AxisBase axisBase) {
                if (v < 0) {
                    return "";
                } else {
                    return xAxisValue.get(((int) v) % xAxisValue.size());
                }
            }
        });

        //y轴设置
        YAxis leftAxis = barChart.getAxisLeft();
        leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        leftAxis.setDrawGridLines(false);
        leftAxis.setDrawLabels(true);
        leftAxis.setDrawAxisLine(true);

        //设置坐标轴最大最小值
        Float yMin1 = Collections.min(yAxisValue1);
        Float yMin2 = Collections.min(yAxisValue2);
        Float yMax1 = Collections.max(yAxisValue1);
        Float yMax2 = Collections.max(yAxisValue2);
        Float yMin = Double.valueOf((yMin1 < yMin2 ? yMin1 : yMin2) * 0.1).floatValue();
        Float yMax = Double.valueOf((yMax1 > yMax2 ? yMax1 : yMax2) * 1.1).floatValue();
        leftAxis.setAxisMaximum(yMax);
        leftAxis.setAxisMinimum(yMin);

        barChart.getAxisRight().setEnabled(false);


        //图例设置
        Legend legend = barChart.getLegend();
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        legend.setDrawInside(false);
        legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);
        legend.setForm(Legend.LegendForm.SQUARE);
        legend.setTextSize(LEGEND_TEXT_SIZE);

        //设置柱状图数据
        setTwoBarChartData(barChart, xAxisValue, yAxisValue1, yAxisValue2, bartilte1, bartitle2, mColors);

        barChart.animateXY(500, 500);//数据显示动画，从左往右依次显示
        barChart.invalidate();
    }

    /**
     * 设置柱状图数据源
     */
    private static void setTwoBarChartData(BarChart barChart, List<String> xAxisValue, List<Float> yAxisValue1, List<Float> yAxisValue2, String bartilte1, String bartitle2, int[] mColors) {
        float groupSpace = 0.2f;
        float barSpace = 0.05f;
        float barWidth = 0.35f;
        // (0.45 + 0.03) * 2 + 0.04 = 1，即一个间隔为一组，包含两个柱图 -> interval per "group"

        ArrayList<BarEntry> entries1 = new ArrayList<>();
        ArrayList<BarEntry> entries2 = new ArrayList<>();

        for (int i = 0, n = yAxisValue1.size(); i < n; ++i) {
            entries1.add(new BarEntry(i, yAxisValue1.get(i)));
            entries2.add(new BarEntry(i, yAxisValue2.get(i)));
        }

        BarDataSet dataset1, dataset2;

        if (barChart.getData() != null && barChart.getData().getDataSetCount() > 0) {
            dataset1 = (BarDataSet) barChart.getData().getDataSetByIndex(0);
            dataset2 = (BarDataSet) barChart.getData().getDataSetByIndex(1);
            dataset1.setValues(entries1);
            dataset2.setValues(entries2);
            barChart.getData().notifyDataChanged();
            barChart.notifyDataSetChanged();
        } else {
            dataset1 = new BarDataSet(entries1, bartilte1);
            dataset2 = new BarDataSet(entries2, bartitle2);

            dataset1.setColor(mColors[0]);
            dataset2.setColor(mColors[1]);

            ArrayList<IBarDataSet> dataSets = new ArrayList<>();
            dataSets.add(dataset1);
            dataSets.add(dataset2);

            BarData data = new BarData(dataSets);
            data.setValueTextSize(VALUE_SIZE);
            data.setBarWidth(0.5f);
            data.setValueFormatter(new IValueFormatter() {
                @Override
                public String getFormattedValue(float value, Entry entry, int i, ViewPortHandler viewPortHandler) {
                    return FormatUtils.format(value, 2) + "";
                }
            });

            barChart.setData(data);
        }

        barChart.getBarData().setBarWidth(barWidth);
        barChart.getXAxis().setAxisMinimum(0);
        // barData.getGroupWith(...) is a helper that calculates the width each group needs based on the provided parameters
        barChart.getXAxis().setAxisMaximum(barChart.getBarData().getGroupWidth(groupSpace, barSpace) * xAxisValue.size() + 0);
        barChart.groupBars(0, groupSpace, barSpace);
    }


    /**
     * 设置三柱状图样式
     */
    public static void setThreeBarChart(BarChart barChart, final List<String> xAxisValue, List<Float> yAxisValue1, List<Float> yAxisValue2, List<Float> yAxisValue3, String bartilte1, String bartitle2, String bartitle3, int[] mColors) {
        barChart.getDescription().setEnabled(false);//设置描述
        barChart.setPinchZoom(false);//设置不按比例放缩柱状图
        barChart.setExtraBottomOffset(10);
        barChart.setExtraTopOffset(30);

        MPChartMarkerView markerView = new MPChartMarkerView(barChart.getContext(), R.layout.custom_marker_view);
        barChart.setMarker(markerView);

        Matrix mMatrix = new Matrix();
        mMatrix.postScale((xAxisValue.size() > 6 ? (float) (Math.ceil(xAxisValue.size() / 6) * (xAxisValue.size() > 20 ? 1.3f : 1f)) : 0f), 0f);
        barChart.getViewPortHandler().refresh(mMatrix, barChart, true);

        //x坐标轴设置
        XAxis xAxis = barChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setGranularity(1f);
        xAxis.setLabelCount(xAxisValue.size());
        xAxis.setCenterAxisLabels(true);
        xAxis.setDrawGridLines(false);
        xAxis.setTextSize(X_TEXT_SIZE - 3);
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float v, AxisBase axisBase) {
                if (v < 0) {
                    return "";
                } else {
                    return xAxisValue.get(((int) v) % xAxisValue.size());
                }
            }
        });

        //y轴设置
        YAxis leftAxis = barChart.getAxisLeft();
        leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        leftAxis.setDrawGridLines(false);
        leftAxis.setDrawLabels(true);
        leftAxis.setDrawAxisLine(true);

        Float yMin1 = Collections.min(yAxisValue1);
        Float yMin2 = Collections.min(yAxisValue2);
        Float yMin3 = Collections.min(yAxisValue3);
        Float yMax1 = Collections.max(yAxisValue1);
        Float yMax2 = Collections.max(yAxisValue2);
        Float yMax3 = Collections.max(yAxisValue3);
        Float yMinTemp = yMin1 < yMin2 ? yMin1 : yMin2;
        Float yMin = yMinTemp < yMin3 ? yMinTemp : yMin3;
        Float yMaxTemp = yMax1 > yMax2 ? yMax1 : yMax2;
        Float yMax = yMaxTemp > yMax3 ? yMaxTemp : yMax3;
        leftAxis.setAxisMinimum(Double.valueOf(yMin * 0.9).floatValue());
        leftAxis.setAxisMaximum(Double.valueOf(yMax * 1.1).floatValue());


        barChart.getAxisRight().setEnabled(false);

        //图例设置
        Legend legend = barChart.getLegend();
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        legend.setDrawInside(false);
        legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);
        legend.setForm(Legend.LegendForm.SQUARE);
        legend.setTextSize(LEGEND_TEXT_SIZE);

        //设置柱状图数据
        setThreeBarChartData(barChart, xAxisValue, yAxisValue1, yAxisValue2, yAxisValue3, bartilte1, bartitle2, bartitle3, mColors);

        barChart.animateY(500);//数据显示动画，从左往右依次显示

        barChart.invalidate();
    }

    /**
     * 设置三柱图数据源
     */
    private static void setThreeBarChartData(BarChart barChart, List<String> xAxisValue, List<Float> yAxisValue1, List<Float> yAxisValue2, List<Float> yAxisValue3, String bartilte1, String bartitle2, String bartitle3, int[] mColors) {
        float groupSpace = 0.1f;
        float barSpace = 0.02f;
        float barWidth = 0.28f;
        // (0.28 + 0.02) * 3 + 0.1 = 1，即一个间隔为一组，包含三个柱图 -> interval per "group"

        ArrayList<BarEntry> first_entries = new ArrayList<>();
        ArrayList<BarEntry> second_entries = new ArrayList<>();
        ArrayList<BarEntry> third_entries = new ArrayList<>();

        for (int i = 0, n = xAxisValue.size(); i < n; ++i) {
            first_entries.add(new BarEntry(i, yAxisValue1.get(i)));
            second_entries.add(new BarEntry(i, yAxisValue2.get(i)));
            third_entries.add(new BarEntry(i, yAxisValue3.get(i)));
        }

        BarDataSet first_set, second_set, third_set;

        if (barChart.getData() != null && barChart.getData().getDataSetCount() > 0) {
            first_set = (BarDataSet) barChart.getData().getDataSetByIndex(0);
            second_set = (BarDataSet) barChart.getData().getDataSetByIndex(1);
            third_set = (BarDataSet) barChart.getData().getDataSetByIndex(2);
            first_set.setValues(first_entries);
            second_set.setValues(second_entries);
            third_set.setValues(third_entries);
            barChart.getData().notifyDataChanged();
            barChart.notifyDataSetChanged();
        } else {
            first_set = new BarDataSet(first_entries, bartilte1);
            second_set = new BarDataSet(second_entries, bartitle2);
            third_set = new BarDataSet(third_entries, bartitle3);

            first_set.setColor(mColors[0]);
            second_set.setColor(mColors[1]);
            third_set.setColor(mColors[2]);

            ArrayList<IBarDataSet> dataSets = new ArrayList<>();
            dataSets.add(first_set);
            dataSets.add(second_set);
            dataSets.add(third_set);

            BarData data = new BarData(dataSets);
            data.setValueTextSize(VALUE_SIZE);
            data.setBarWidth(0.9f);
            data.setValueFormatter(new IValueFormatter() {
                @Override
                public String getFormattedValue(float value, Entry entry, int i, ViewPortHandler viewPortHandler) {
                    return FormatUtils.format(value, 2) + "";
                }
            });

            barChart.setData(data);
        }

        barChart.getBarData().setBarWidth(barWidth);
        barChart.getXAxis().setAxisMinimum(0);
        barChart.getXAxis().setAxisMaximum(barChart.getBarData().getGroupWidth(groupSpace, barSpace) * xAxisValue.size() + 0);
        barChart.groupBars(0, groupSpace, barSpace);
    }


    /**
     * 单线单y轴图。
     *
     * @param showSetValues 是否在折线上显示数据集的值。true为显示，此时y轴上的数值不可见，否则相反。
     */
    public static void setLineChart(LineChart lineChart, List<String> xAxisValue, List<Float> yAxisValue, String title, boolean showSetValues, int color) {
        List<List<Float>> entriesList = new ArrayList<>();
        entriesList.add(yAxisValue);

        List<String> titles = new ArrayList<>();
        titles.add(title);

        if (color != 0) {
            int[] mColor = {color};
            setLinesChart(lineChart, xAxisValue, entriesList, titles, showSetValues, mColor);
        } else {
            setLinesChart(lineChart, xAxisValue, entriesList, titles, showSetValues, null);
        }
    }

    /**
     * 绘制线图，默认最多绘制三种颜色。所有线均依赖左侧y轴显示。
     *
     * @param xAxisValue    x轴的轴
     * @param yXAxisValues  y轴的值
     * @param titles        每一个数据系列的标题
     * @param showSetValues 是否在折线上显示数据集的值。true为显示，此时y轴上的数值不可见，否则相反。
     * @param lineColors    线的颜色数组。为null时取默认颜色，此时最多绘制三种颜色。
     */
    public static void setLinesChart(LineChart lineChart, List<String> xAxisValue, List<List<Float>> yXAxisValues, List<String> titles, boolean showSetValues, int[] lineColors) {
        lineChart.getDescription().setEnabled(false);//设置描述
        lineChart.setPinchZoom(false);
//        lineChart.setTouchEnabled(false);
        // scaling can now only be done on x- and y-axis separately
//        lineChart.setPinchZoom(false);
        lineChart.setScaleEnabled(false);

//        if (!showSetValues) {
//            MPChartMarkerView markerView = new MPChartMarkerView(lineChart.getContext(), R.layout.custom_marker_view);
//            lineChart.setMarker(markerView);
//        }

        Matrix mMatrix = new Matrix();
        mMatrix.postScale((xAxisValue.size() > 6 ? (float) (Math.ceil(xAxisValue.size() / 6) * (xAxisValue.size() > 20 ? 1.4f : 1.2f)) : 0f), 0f);
        lineChart.getViewPortHandler().refresh(mMatrix, lineChart, true);

        //x坐标轴设置
        IAxisValueFormatter xAxisFormatter = new StringAxisValueFormatter(xAxisValue);
        XAxis xAxis = lineChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        xAxis.setGranularity(1f);
        xAxis.setLabelCount((xAxisValue.size() > 12 ? 12 : xAxisValue.size()));
        xAxis.setTextSize(X_TEXT_SIZE);
        /*xAxis.setAxisLineWidth(2f);*/
        xAxis.setValueFormatter(xAxisFormatter);

        //y轴设置
        YAxis leftAxis = lineChart.getAxisLeft();
        leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        leftAxis.setDrawGridLines(false);
        if (showSetValues) {
            leftAxis.setDrawLabels(true);//折线上显示值，则不显示坐标轴上的值
        }
//        leftAxis.setDrawZeroLine(false);
        /*leftAxis.setAxisMinimum(0f);*/
        /*leftAxis.setAxisLineWidth(2f);*/

        lineChart.getAxisRight().setEnabled(false);

        //图例设置
        Legend legend = lineChart.getLegend();
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        legend.setDrawInside(false);
        legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);
        legend.setForm(Legend.LegendForm.LINE);
        legend.setTextSize(LEGEND_TEXT_SIZE);

        //设置柱状图数据
        setLinesChartData(lineChart, yXAxisValues, titles, showSetValues, lineColors);

        lineChart.setExtraOffsets(10, 30, 20, 10);
        lineChart.animateY(500);//数据显示动画，从左往右依次显示
    }

    public static void setLinesChartData(LineChart lineChart, List<List<Float>> yXAxisValues, List<String> titles, boolean showSetValues, int[] lineColors) {
        List<List<Entry>> entriesList = new ArrayList<>();
        for (int i = 0; i < yXAxisValues.size(); ++i) {
            ArrayList<Entry> entries = new ArrayList<>();
            for (int j = 0, n = yXAxisValues.get(i).size(); j < n; j++) {
                entries.add(new Entry(j, yXAxisValues.get(i).get(j)));
            }
            entriesList.add(entries);
        }

        if (lineChart.getData() != null && lineChart.getData().getDataSetCount() > 0) {

            for (int i = 0; i < lineChart.getData().getDataSetCount(); ++i) {
                LineDataSet set = (LineDataSet) lineChart.getData().getDataSetByIndex(i);
                set.setValues(entriesList.get(i));
                set.setLabel(titles.get(i));
            }

            lineChart.getData().notifyDataChanged();
            lineChart.notifyDataSetChanged();
        } else {
            ArrayList<ILineDataSet> dataSets = new ArrayList<>();

            for (int i = 0; i < entriesList.size(); ++i) {
                LineDataSet set = new LineDataSet(entriesList.get(i), titles.get(i));
                if (lineColors != null) {
                    set.setColor(lineColors[i % entriesList.size()]);
                    set.setCircleColor(lineColors[i % entriesList.size()]);
                    set.setCircleColorHole(Color.WHITE);
                } else {
                    set.setColor(LINE_COLORS[i % 3]);
                    set.setCircleColor(LINE_COLORS[i % 3]);
                    set.setCircleColorHole(Color.WHITE);
                }

                if (entriesList.size() == 1 && lineColors != null) {
                    set.setDrawFilled(true);
                    set.setFillAlpha(10);
                    set.setFillColor(lineColors[i % entriesList.size()]);
                }
                set.setLineWidth(1f);
//                set.setMode(LineDataSet.Mode.CUBIC_BEZIER);
                dataSets.add(set);
            }

            LineData data = new LineData(dataSets);
            if (showSetValues) {
                data.setValueTextSize(10f);
                data.setValueFormatter(new IValueFormatter() {
                    @Override
                    public String getFormattedValue(float value, Entry entry, int i, ViewPortHandler viewPortHandler) {
                        return FormatUtils.format(value, 2) + "";
                    }
                });
            } else {
                data.setDrawValues(false);
            }

            lineChart.setData(data);
        }
    }


    /**
     * 设置饼图样式
     *
     * @param showLegend 是否显示图例
     */
    public static void setPieChart(PieChart pieChart, LinkedHashMap<String, Float> pieValues, String title, boolean showLegend) {
        pieChart.setUsePercentValues(true);//设置使用百分比
        pieChart.getDescription().setEnabled(false);//设置描述
        pieChart.setExtraOffsets(0, 10, 45, 10);
        pieChart.setCenterText(title);//设置环中的文字
        pieChart.setCenterTextSize(22f);//设置环中文字的大小
        pieChart.setDrawCenterText(true);//设置绘制环中文字
        pieChart.setRotationAngle(120f);//设置旋转角度
        pieChart.setDrawEntryLabels(false);
        pieChart.setRotationEnabled(false);//设置旋转

        //图例设置
        Legend legend = pieChart.getLegend();
        if (showLegend) {
            legend.setEnabled(true);
            legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
            legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
            legend.setOrientation(Legend.LegendOrientation.VERTICAL);
            legend.setDrawInside(false);
            legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);
            legend.setWordWrapEnabled(true);//换行
        } else {
            legend.setEnabled(false);
        }

        //设置饼图数据
        setPieChartData(pieChart, pieValues);

        pieChart.animateX(1500, Easing.EasingOption.EaseInOutQuad);//数据显示动画
    }

    /**
     * 设置饼图数据源
     */
    private static void setPieChartData(PieChart pieChart, LinkedHashMap<String, Float> pieValues) {
        pieChart.clear();
        ArrayList<PieEntry> entries = new ArrayList<>();

        Set set = pieValues.entrySet();
        Iterator it = set.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            entries.add(new PieEntry(Float.valueOf(entry.getValue().toString()), entry.getKey().toString()));
        }

        PieDataSet dataSet = new PieDataSet(entries, "");
        dataSet.setSliceSpace(0f);//设置饼块之间的间隔
        dataSet.setSelectionShift(2f);//设置饼块选中时偏离饼图中心的距离

        dataSet.setColors(PIE_COLORS);//设置饼块的颜色
        dataSet.setValueLinePart1OffsetPercentage(80f);//数据连接线距图形片内部边界的距离，为百分数
        dataSet.setValueLinePart1Length(0.3f);
        dataSet.setValueLinePart2Length(0.4f);
        dataSet.setValueLineColor(Color.BLACK);//设置连接线的颜色
        dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
//        dataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);

        PieData pieData = new PieData(dataSet);
        pieData.setValueFormatter(new PercentFormatter());
        pieData.setValueTextSize(11f);
        pieData.setValueTextColor(Color.DKGRAY);

        pieChart.setData(pieData);
        pieChart.highlightValues(null);
        pieChart.invalidate();
    }


    /**
     * 设置柱线组合图样式，柱图依赖左侧y轴，线图依赖右侧y轴
     */
    public static void setCombineChart(CombinedChart combineChart, final List<String> xAxisValues, List<Float> lineValues, List<Float> barValues, String lineTitle, String barTitle, int lineColor, int barColor) {
        combineChart.getDescription().setEnabled(false);//设置描述
        combineChart.setPinchZoom(true);//设置按比例放缩柱状图
        combineChart.setScaleEnabled(false);

        MPChartMarkerView markerView = new MPChartMarkerView(combineChart.getContext(), R.layout.custom_marker_view);
        combineChart.setMarker(markerView);

        Matrix mMatrix = new Matrix();
        mMatrix.postScale((xAxisValues.size() > 6 ? (float) (Math.ceil(xAxisValues.size() / 6) * (xAxisValues.size() > 20 ? 1.3f : 1f)) : 0f), 0f);
        combineChart.getViewPortHandler().refresh(mMatrix, combineChart, true);

        //设置绘制顺序，让线在柱的上层
        combineChart.setDrawOrder(new CombinedChart.DrawOrder[]{
                CombinedChart.DrawOrder.BAR, CombinedChart.DrawOrder.LINE
        });

        //x坐标轴设置
        XAxis xAxis = combineChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        xAxis.setGranularity(1);
        xAxis.setAxisMinimum(-1);
        xAxis.setTextSize(X_TEXT_SIZE - 2);
//        xAxis.setCenterAxisLabels(true); //x轴值居中
        xAxis.setLabelCount(xAxisValues.size() + 2);
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float v, AxisBase axisBase) {
                if (v >= 0 && v <= (xAxisValues.size() - 1)) {
                    return xAxisValues.get((int) v);
                }
                return "";
            }
        });

        //y轴设置
        YAxis leftAxis = combineChart.getAxisLeft();
        leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        leftAxis.setDrawGridLines(false);
        /*leftAxis.setAxisMinimum(0f);*/

        Float yMin = Double.valueOf(Collections.min(barValues) * 0.9).floatValue();
        Float yMax = Double.valueOf(Collections.max(barValues) * 1.1).floatValue();
        leftAxis.setAxisMaximum(yMax);
        leftAxis.setAxisMinimum(yMin);

        YAxis rightAxis = combineChart.getAxisRight();
        rightAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        rightAxis.setGranularity(1f);
        Float yLineMin = Double.valueOf(Collections.min(lineValues) - 10).floatValue();
        Float yLineMax = Double.valueOf(Collections.max(lineValues) + 10).floatValue();
        rightAxis.setAxisMaximum(yLineMax);
        rightAxis.setAxisMinimum(yLineMin);
        rightAxis.setDrawGridLines(false);

        //图例设置
        Legend legend = combineChart.getLegend();
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        legend.setDrawInside(false);
        legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);
        legend.setForm(Legend.LegendForm.SQUARE);
        legend.setTextSize(LEGEND_TEXT_SIZE);

        //设置组合图数据
        CombinedData data = new CombinedData();
        data.setData(generateLineData(lineValues, lineTitle, lineColor));
        data.setData(generateBarData(barValues, barTitle, barColor));

        combineChart.setData(data);//设置组合图数据源


        //使得两侧柱子完全显示
        xAxis.setAxisMinimum(combineChart.getCombinedData().getXMin() - 1f);
        xAxis.setAxisMaximum(combineChart.getCombinedData().getXMax() + 1f);

        combineChart.setExtraTopOffset(30);
        combineChart.setExtraBottomOffset(10);
        combineChart.animateY(500);//数据显示动画，从左往右依次显示
        combineChart.invalidate();
    }

    /**
     * 生成线图数据
     */
    private static LineData generateLineData(List<Float> lineValues, String lineTitle, int lineColor) {
        ArrayList<Entry> lineEntries = new ArrayList<>();

        for (int i = 0, n = lineValues.size(); i < n; ++i) {
            lineEntries.add(new Entry(i, lineValues.get(i)));
        }

        LineDataSet lineDataSet = new LineDataSet(lineEntries, lineTitle);
        lineDataSet.setColor(lineColor);
        //设置线的宽度
        lineDataSet.setLineWidth(1f);
        //设置圆圈的颜色
        lineDataSet.setCircleColor(lineColor);
        //设置圆圈内部洞的颜色
        lineDataSet.setCircleColorHole(Color.WHITE);
        //lineDataSet.setValueTextColor(Color.rgb(254,116,139));
        //设置线数据依赖于右侧y轴
        lineDataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
        //不绘制线的数据
        lineDataSet.setDrawValues(true);
        lineDataSet.setHighlightEnabled(false);

        LineData lineData = new LineData(lineDataSet);
        lineData.setValueTextSize(10f);
        lineData.setValueFormatter(new IValueFormatter() {
            @Override
            public String getFormattedValue(float value, Entry entry, int i, ViewPortHandler viewPortHandler) {
                return FormatUtils.format(value, 2) + "";
            }
        });

        return lineData;
    }

    /**
     * 生成柱图数据
     */
    private static BarData generateBarData(List<Float> barValues, String barTitle, int barColor) {

        ArrayList<BarEntry> barEntries = new ArrayList<>();

        for (int i = 0, n = barValues.size(); i < n; ++i) {
            barEntries.add(new BarEntry(i, barValues.get(i)));
        }

        BarDataSet barDataSet = new BarDataSet(barEntries, barTitle);
        barDataSet.setColor(barColor);
        barDataSet.setValueTextColor(barColor);
        barDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
        barDataSet.setHighlightEnabled(true);
        barDataSet.setDrawValues(false);

        BarData barData = new BarData(barDataSet);
        barData.setValueTextSize(10f);
        barData.setBarWidth(0.6f);
        barData.setValueFormatter(new IValueFormatter() {
            @Override
            public String getFormattedValue(float value, Entry entry, int i, ViewPortHandler viewPortHandler) {
                return FormatUtils.format(value, 2) + "";
            }
        });

        return barData;
    }

    /**
     * 设置横向柱图样式
     */
    public static void setStackedBarChart(HorizontalBarChart barChart, final List<String> xAxisValue, List<float[]> yAxisValue, String title, Integer barColor) {

        barChart.setDrawGridBackground(false);
        barChart.getDescription().setEnabled(false);
        barChart.setTouchEnabled(false);
        // scaling can now only be done on x- and y-axis separately
        barChart.setPinchZoom(false);
        barChart.setScaleEnabled(false);

        barChart.setDrawBarShadow(false);
        barChart.setDrawValueAboveBar(true);
        barChart.setHighlightFullBarEnabled(false);

        ArrayList<Float> yVal = new ArrayList<>();
        for (int i = 0; i < yAxisValue.size(); i++) {
            yVal.add(yAxisValue.get(i)[0]);
            yVal.add(yAxisValue.get(i)[1]);
        }

        Float yMax = Collections.max(yVal);
        barChart.getAxisLeft().setEnabled(false);
        barChart.getAxisRight().setDrawGridLines(false);
        barChart.getAxisRight().setDrawZeroLine(true);
        float addMax = 40;
        barChart.getAxisRight().setAxisMaximum(yMax + addMax);
        barChart.getAxisRight().setAxisMinimum(-(yMax + addMax));
        barChart.getAxisRight().setLabelCount(7, false);
        barChart.getAxisRight().setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return Math.abs(value) + "";
            }
        });
        barChart.getAxisRight().setTextSize(7f);
        barChart.getAxisRight().setEnabled(true);


        //x坐标轴设置
        IAxisValueFormatter xAxisFormatter = new StringAxisValueFormatter(xAxisValue);//设置自定义的x轴值格式化器
        final XAxis xAxis = barChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTH_SIDED);
//        xAxis.setAxisMinimum(0f);
        xAxis.setDrawGridLines(false);
        xAxis.setDrawAxisLine(false);
        xAxis.setTextSize(10f);
        xAxis.setCenterAxisLabels(true);
        xAxis.setLabelCount(xAxisValue.size());
        xAxis.setGranularity(10f);
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return xAxisValue.get(((int) value) % xAxisValue.size());
            }
        });//设置自定义的x轴值格式化器
        xAxis.setEnabled(true);

        //图例设置
        Legend legend = barChart.getLegend();
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);//图例水平居中
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);//图例在图表上方
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);//图例的方向为水平
        legend.setDrawInside(false);//绘制在chart的外侧
        legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);//图例中的文字方向

        legend.setForm(Legend.LegendForm.SQUARE);//图例窗体的形状
        legend.setFormSize(LEGEND_SIZE);//图例窗体的大小
        legend.setTextSize(LEGEND_TEXT_SIZE);//图例文字的大小

        //设置柱状图数据
        setStackedBarChartData(barChart, yAxisValue, title, barColor);

//        barChart.setExtraBottomOffset(10);//距视图窗口底部的偏移，类似与paddingbottom
//        barChart.setExtraTopOffset(30);//距视图窗口顶部的偏移，类似与paddingtop
//        barChart.setExtraLeftOffset(30);
//        barChart.setExtraRightOffset(30);
        barChart.setFitBars(true);//使两侧的柱图完全显示
        barChart.animateY(500);//数据显示动画，从左往右依次显示

    }


    /**
     * 设置横向柱图
     */
    private static void setStackedBarChartData(HorizontalBarChart barChart, List<float[]> yAxisValue, String title, Integer barColor) {

        ArrayList<BarEntry> entries = new ArrayList<>();
        int position = 5;
        for (int i = 0, n = yAxisValue.size(); i < n; ++i) {
            float[] floats = yAxisValue.get(i);
            floats[0] = -floats[0];
            entries.add(new BarEntry(position, floats));
            position += 10;
        }

        BarDataSet barDataSet;

        if (barChart.getData() != null && barChart.getData().getDataSetCount() > 0) {
            barDataSet = (BarDataSet) barChart.getData().getDataSetByIndex(0);
            barDataSet.setValues(entries);
            barChart.getData().notifyDataChanged();
            barChart.notifyDataSetChanged();
        } else {
            barDataSet = new BarDataSet(entries, title);
            barDataSet.setColors(new int[]{Color.rgb(67, 67, 72), Color.rgb(124, 181, 236)});
            barDataSet.setStackLabels(new String[]{
                    "本行", "对标行"
            });
            barDataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
            ArrayList<IBarDataSet> dataSets = new ArrayList<>();
            dataSets.add(barDataSet);

            BarData data = new BarData(dataSets);
            data.setValueTextSize(8f);
            data.setBarWidth(7f);
            data.setValueFormatter(new IValueFormatter() {
                @Override
                public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
                    return FormatUtils.format(Math.abs(value), 2);
                }
            });

            barChart.setData(data);
        }
    }


    /**
     * 设置柱散点图组合图样式，柱图依赖左侧y轴，散点图依赖右侧y轴
     */
    public static void setCombineScatterChart(CombinedChart combineChart, final List<String> xAxisValues, List<Float> scatterValues, List<Float> barValues, String scatterTitle, String barTitle, int barColor, int scatterColor) {
        combineChart.getDescription().setEnabled(false);//设置描述
        combineChart.setPinchZoom(false);//设置按比例放缩柱状图
        combineChart.setTouchEnabled(false);

        MPChartMarkerView markerView = new MPChartMarkerView(combineChart.getContext(), R.layout.custom_marker_view);
        combineChart.setMarker(markerView);

        //设置绘制顺序，让线在柱的上层
        combineChart.setDrawOrder(new CombinedChart.DrawOrder[]{
                CombinedChart.DrawOrder.BAR, CombinedChart.DrawOrder.SCATTER
        });

        Matrix mMatrix = new Matrix();
        mMatrix.postScale((xAxisValues.size() > 6 ? (float) (Math.ceil(xAxisValues.size() / 6) * (xAxisValues.size() > 20 ? 1.3f : 1f)) : 0f), 0f);
        combineChart.getViewPortHandler().refresh(mMatrix, combineChart, true);

        //x坐标轴设置
        XAxis xAxis = combineChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        xAxis.setGranularity(1f);
        xAxis.setTextSize(X_TEXT_SIZE - 4);
        xAxis.setLabelCount(xAxisValues.size() + 2);
        xAxis.setValueFormatter(new IAxisValueFormatter() {

            @Override
            public String getFormattedValue(float v, AxisBase axisBase) {
                if (v >= 0 && v <= (xAxisValues.size() - 1)) {
                    return xAxisValues.get((int) v);
                }
                return "";
            }
        });

        //y轴设置
        YAxis leftAxis = combineChart.getAxisLeft();
        leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        leftAxis.setDrawGridLines(false);
        /*leftAxis.setAxisMinimum(0f);*/

        Float yMin = Double.valueOf(Collections.min(barValues) * 0.9).floatValue();
        Float yMax = Double.valueOf(Collections.max(barValues) * 1.1).floatValue();
        leftAxis.setAxisMaximum(yMax);
        leftAxis.setAxisMinimum(yMin);


        YAxis rightAxis = combineChart.getAxisRight();
        rightAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        rightAxis.setDrawGridLines(false);

        //图例设置
        Legend legend = combineChart.getLegend();
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        legend.setDrawInside(false);
        legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);
        legend.setForm(Legend.LegendForm.SQUARE);
        legend.setTextSize(LEGEND_TEXT_SIZE);

        //设置组合图数据
        CombinedData data = new CombinedData();
        data.setData(getScatterData(scatterValues, scatterTitle, scatterColor));
        data.setData(getBarData(barValues, barTitle, barColor));

        combineChart.setData(data);//设置组合图数据源


        //使得两侧柱子完全显示
        xAxis.setAxisMinimum(combineChart.getCombinedData().getXMin() - 1f);
        xAxis.setAxisMaximum(combineChart.getCombinedData().getXMax() + 1f);

        combineChart.setExtraTopOffset(10);
        combineChart.setExtraBottomOffset(10);
        combineChart.animateX(500);//数据显示动画，从左往右依次显示
        combineChart.invalidate();
    }

    /**
     * 生成柱图数据
     */
    private static BarData getBarData(List<Float> barValues, String barTitle, int barColor) {

        ArrayList<BarEntry> barEntries = new ArrayList<>();

        for (int i = 0, n = barValues.size(); i < n; ++i) {
            barEntries.add(new BarEntry(i, barValues.get(i)));
        }

        BarDataSet barDataSet = new BarDataSet(barEntries, barTitle);
        barDataSet.setColor(barColor);
        barDataSet.setValueTextColor(barColor);
        barDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
        barDataSet.setHighlightEnabled(false);

        BarData barData = new BarData(barDataSet);
        barData.setValueTextSize(10f);
        barData.setBarWidth(0.9f);
        barData.setValueFormatter(new IValueFormatter() {
            @Override
            public String getFormattedValue(float value, Entry entry, int i, ViewPortHandler viewPortHandler) {
                return FormatUtils.format(value, 2) + "";
            }
        });

        return barData;
    }

    /**
     * 生成散点图数据
     */
    private static ScatterData getScatterData(List<Float> scatterValues, String title, int scatterColor) {
        ArrayList<Entry> scatterEntries = new ArrayList<>();

        for (int i = 0, n = scatterValues.size(); i < n; ++i) {
            scatterEntries.add(new Entry(i, scatterValues.get(i)));
        }

        ScatterDataSet scatterDataSet = new ScatterDataSet(scatterEntries, title);
        scatterDataSet.setColor(scatterColor);
        scatterDataSet.setScatterShape(ScatterChart.ScatterShape.CIRCLE);
        scatterDataSet.setScatterShapeSize(40);
        scatterDataSet.setValueFormatter(new IValueFormatter() {
            @Override
            public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
                return FormatUtils.format(value * 100, 2) + "%";
            }
        });
        scatterDataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);//设置线数据依赖于右侧y轴
        scatterDataSet.setDrawValues(false);//绘制散点的数据

        ScatterData scatterData = new ScatterData(scatterDataSet);
        scatterData.setValueTextSize(7f);
        scatterData.setValueTextColor(scatterColor);
        scatterData.setValueFormatter(new IValueFormatter() {
            @Override
            public String getFormattedValue(float value, Entry entry, int i, ViewPortHandler viewPortHandler) {
                return FormatUtils.format(value, 2);
            }
        });
        return scatterData;
    }


    /**
     * 设置横向叠加柱图样式
     */
    public static void setHorizontalStackBarChart(HorizontalBarChart barChart, final List<String> xAxisValue, List<float[]> yAxisValue, String title, int[] barColors, String[] labels) {

        barChart.setDrawGridBackground(false);
        barChart.getDescription().setEnabled(false);
        barChart.setTouchEnabled(true);
        // scaling can now only be done on x- and y-axis separately
        barChart.setPinchZoom(true);
        barChart.setScaleEnabled(false);

        MPChartMarkerView markerView = new MPChartMarkerView(barChart.getContext(), R.layout.custom_marker_view);
        barChart.setMarker(markerView);

        barChart.setDrawBarShadow(false);
        barChart.setDrawValueAboveBar(true);
        barChart.setHighlightFullBarEnabled(false);

        ArrayList<Float> yVal = new ArrayList<>();
        for (int i = 0; i < yAxisValue.size(); i++) {
            float sum = 0;
            for (int j = 0; j < yAxisValue.get(i).length; j++) {
                sum += yAxisValue.get(i)[j];
            }
            yVal.add(sum);
        }

        Float yMax = Collections.max(yVal);
        barChart.getAxisLeft().setDrawGridLines(false);
        barChart.getAxisLeft().setDrawZeroLine(true);
        float addMax = yMax / 5;
        barChart.getAxisLeft().setAxisMaximum(yMax + addMax);
        barChart.getAxisLeft().setAxisMinimum(0f);
        barChart.getAxisLeft().setLabelCount(4, false);
        barChart.getAxisLeft().setValueFormatter(new IAxisValueFormatter() {

            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return Math.abs(value) + "";
            }
        });
        barChart.getAxisLeft().setTextSize(8f);
        barChart.getAxisLeft().setEnabled(false);
        barChart.getAxisRight().setDrawGridLines(false);
        barChart.getAxisRight().setDrawZeroLine(true);
        barChart.getAxisRight().setAxisMaximum(yMax + addMax);
        barChart.getAxisRight().setAxisMinimum(0f);
        barChart.getAxisRight().setLabelCount(4, false);
        barChart.getAxisRight().setValueFormatter(new IAxisValueFormatter() {

            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return Math.abs(value) + "";
            }
        });
        barChart.getAxisRight().setTextSize(8f);
        barChart.getAxisRight().setEnabled(true);

        barChart.getAxisRight().setSpaceTop(20f);


        //x坐标轴设置
        final XAxis xAxis = barChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setAxisMinimum(0f);
        xAxis.setDrawGridLines(false);
        xAxis.setDrawAxisLine(false);
        xAxis.setTextSize(10f);
        xAxis.setCenterAxisLabels(true);
        xAxis.setLabelCount(xAxisValue.size() - 1);
        xAxis.setGranularity(10f);
        xAxis.setValueFormatter(new IAxisValueFormatter() {

            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                int index = ((int) value) / 10;
                if (value < 0 || index >= xAxisValue.size()) {
                    return "";
                }
                return xAxisValue.get(index);
            }
        });//设置自定义的x轴值格式化器
        xAxis.setEnabled(true);

        //图例设置
        Legend legend = barChart.getLegend();
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);//图例水平居中
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);//图例在图表上方
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);//图例的方向为水平
        legend.setDrawInside(false);//绘制在chart的外侧
        legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);//图例中的文字方向

        legend.setForm(Legend.LegendForm.SQUARE);//图例窗体的形状
        legend.setFormSize(LEGEND_SIZE);//图例窗体的大小
        legend.setTextSize(LEGEND_TEXT_SIZE);//图例文字的大小

        //设置柱状图数据
        setHorizontalStackBarData(barChart, yAxisValue, title, barColors, labels);

//        barChart.setExtraBottomOffset(10);//距视图窗口底部的偏移，类似与paddingbottom
//        barChart.setExtraTopOffset(30);//距视图窗口顶部的偏移，类似与paddingtop
//        barChart.setExtraLeftOffset(30);
//        barChart.setExtraRightOffset(30);
        barChart.setFitBars(true);//使两侧的柱图完全显示
        barChart.animateY(500);//数据显示动画，从左往右依次显示

    }


    /**
     * 设置横向柱图数据
     */
    private static void setHorizontalStackBarData(HorizontalBarChart barChart, List<float[]> yAxisValue, String title, int[] barColors, String[] labels) {
        float groupSpace = 0.1f;
        float barSpace = 0.02f;
        float barWidth = 0.28f;
        // (0.28 + 0.02) * 3 + 0.1 = 1，即一个间隔为一组，包含三个柱图 -> interval per "group"

        ArrayList<BarEntry> entries = new ArrayList<>();
        int position = 5;
        for (int i = 0, n = yAxisValue.size(); i < n; ++i) {
            float[] floats = yAxisValue.get(i);
            entries.add(new BarEntry(position, floats));
            position += 10;
        }

        BarDataSet barDataSet;

        if (barChart.getData() != null && barChart.getData().getDataSetCount() > 0) {
            barDataSet = (BarDataSet) barChart.getData().getDataSetByIndex(0);
            barDataSet.setValues(entries);
            barChart.getData().notifyDataChanged();
            barChart.notifyDataSetChanged();
        } else {
            barDataSet = new BarDataSet(entries, title);
            if (barColors == null) {
                barDataSet.setColors(BAR_COLORS);
            } else {
                barDataSet.setColors(barColors);
            }

            barDataSet.setStackLabels(labels);
            barDataSet.setDrawValues(false);
            barDataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
            ArrayList<IBarDataSet> dataSets = new ArrayList<>();
            dataSets.add(barDataSet);

            BarData data = new BarData(dataSets);
            data.setValueTextSize(8f);
            data.setBarWidth(7f);
            data.setValueFormatter(new IValueFormatter() {
                @Override
                public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
                    return FormatUtils.format(Math.abs(value), 2);
                }
            });

            barChart.setData(data);
        }
    }

    /**
     * 设置竖向叠加柱图样式
     */
    public static void setVerticalStackBarChart(BarChart barChart, final List<String> xAxisValue, List<float[]> yAxisValue, String title, int[] barColors, String[] labels) {

        barChart.getDescription().setEnabled(false);//设置描述
        barChart.setPinchZoom(false);//设置不按比例放缩柱状图
        barChart.setScaleEnabled(false);
        MPChartMarkerView markerView = new MPChartMarkerView(barChart.getContext(), R.layout.custom_marker_view);
        barChart.setMarker(markerView);

        Matrix mMatrix = new Matrix();
        mMatrix.postScale((xAxisValue.size() > 6 ? (float) (Math.ceil(xAxisValue.size() / 6) * (xAxisValue.size() > 20 ? 1.3f : 1f)) : 0f), 0f);
        barChart.getViewPortHandler().refresh(mMatrix, barChart, true);

        //x坐标轴设置
        XAxis xAxis = barChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        xAxis.setGranularity(1f);
        xAxis.setTextSize(X_TEXT_SIZE - 3);
        xAxis.setLabelCount(xAxisValue.size() + 2);
//        xAxis.setCenterAxisLabels(true);//设置标签居中
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float v, AxisBase axisBase) {
                if (v < 0 || v >= xAxisValue.size()) {
                    return "";
                } else {
                    String s = xAxisValue.get(((int) v) % xAxisValue.size());
                    return s;
                }
            }
        });

        //y轴设置
        YAxis axisLeft = barChart.getAxisLeft();
//        axisLeft.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        axisLeft.setDrawGridLines(true);
        axisLeft.setDrawLabels(true);
        axisLeft.setDrawAxisLine(true);
        axisLeft.setEnabled(true);
        axisLeft.setLabelCount(5);
        axisLeft.setDrawZeroLine(true);
        axisLeft.setAxisMinimum(0);

        barChart.getAxisRight().setEnabled(false);

        ArrayList<Float> yVal = new ArrayList<>();
        for (int i = 0; i < yAxisValue.size(); i++) {
            float sum = 0;
            for (int j = 0; j < yAxisValue.get(i).length; j++) {
                sum += yAxisValue.get(i)[j];
            }
            yVal.add(sum);
        }

        Float yMax = Collections.max(yVal);
        axisLeft.setAxisMaximum(yMax * 1.1f);

        //图例设置
        Legend legend = barChart.getLegend();
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        legend.setDrawInside(false);
        legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);
        legend.setForm(Legend.LegendForm.SQUARE);
        legend.setTextSize(LEGEND_TEXT_SIZE);

        //设置柱状图数据
        setVerticalStackBarData(barChart, yAxisValue, title, barColors, labels);

        barChart.animateY(500);//数据显示动画，从左往右依次显示
    }

    /**
     * 设置竖向柱图数据
     */
    private static void setVerticalStackBarData(BarChart barChart, List<float[]> yAxisValue, String title, int[] barColors, String[] labels) {

        ArrayList<BarEntry> entries = new ArrayList<>();
        for (int i = 0, n = yAxisValue.size(); i < n; ++i) {
            float[] floats = yAxisValue.get(i);
            entries.add(new BarEntry(i, floats));
        }

        BarDataSet barDataSet;

        if (barChart.getData() != null && barChart.getData().getDataSetCount() > 0) {
            barDataSet = (BarDataSet) barChart.getData().getDataSetByIndex(0);
            barDataSet.setValues(entries);
            barChart.getData().notifyDataChanged();
            barChart.notifyDataSetChanged();
        } else {
            barDataSet = new BarDataSet(entries, title);
            if (barColors == null) {
                barDataSet.setColors(BAR_COLORS);
            } else {
                barDataSet.setColors(barColors);
            }

            barDataSet.setStackLabels(labels);
            barDataSet.setDrawValues(false);
            barDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
            ArrayList<IBarDataSet> dataSets = new ArrayList<>();
            dataSets.add(barDataSet);

            BarData data = new BarData(dataSets);
            data.setValueTextSize(8f);
            data.setBarWidth(0.55f);
            data.setValueFormatter(new IValueFormatter() {
                @Override
                public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
                    return FormatUtils.format(Math.abs(value), 2);
                }
            });
            barChart.setData(data);
        }
    }


    /**
     * 设置雷达图样式
     */
    public static void setRadarChart(RadarChart radarChart, final List<String> xAxisValue, List<List<Float>> yXAxisValues, List<String> titles, boolean showSetValues, int[] mColors, int[] fillColors) {

//        radarChart.setDrawGridBackground(false);
        radarChart.getDescription().setEnabled(false);
        radarChart.setTouchEnabled(true);

        radarChart.getDescription().setEnabled(false);

        radarChart.setRotationEnabled(false);
        radarChart.setWebLineWidth(1f);
        radarChart.setWebColor(Color.LTGRAY);
        radarChart.setWebLineWidthInner(0.5f);
        radarChart.setWebColorInner(Color.LTGRAY);
        radarChart.setWebAlpha(180);

//        MPChartMarkerView markerView = new MPChartMarkerView(radarChart.getContext(), R.layout.custom_marker_view);
//        radarChart.setMarker(markerView);

        //y坐标轴设置
        YAxis yAxis = radarChart.getYAxis();
        yAxis.setLabelCount(5, false);
        yAxis.setTextSize(6f);
        yAxis.setAxisMinimum(0f);
        yAxis.setDrawLabels(true);
        yAxis.setAxisLineWidth(0.2f);

        //x坐标轴设置
        final XAxis xAxis = radarChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setTextSize(X_TEXT_SIZE - 3);
        xAxis.setAxisMinimum(0f);
        xAxis.setCenterAxisLabels(true);
        xAxis.setLabelCount(xAxisValue.size());
        xAxis.setGranularity(10f);
//        xAxis.setLabelCount(4);
        xAxis.setValueFormatter(new IAxisValueFormatter() {

            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return xAxisValue.get(((int) value) % xAxisValue.size());
            }
        });//设置自定义的x轴值格式化器
        xAxis.setEnabled(true);

        //图例设置
        Legend legend = radarChart.getLegend();
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);//图例水平居中
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);//图例在图表上方
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);//图例的方向为水平
        legend.setDrawInside(false);//绘制在chart的外侧
        legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);//图例中的文字方向

        legend.setForm(Legend.LegendForm.SQUARE);//图例窗体的形状
        legend.setFormSize(LEGEND_SIZE);//图例窗体的大小
        legend.setTextSize(LEGEND_TEXT_SIZE);//图例文字的大小

        //设置柱状图数据
        setHorizontalStackBarData(radarChart, yXAxisValues, titles, showSetValues, mColors, fillColors);

//        radarChart.setExtraTopOffset(30);//距视图窗口顶部的偏移，类似与paddingtop
//        radarChart.setFitBars(true);//使两侧的柱图完全显示
        radarChart.animateXY(500, 500);//数据显示动画，从左往右依次显示

    }


    /**
     * 设置雷达图数据
     */
    private static void setHorizontalStackBarData(RadarChart radarChart, List<List<Float>> yXAxisValues, List<String> titles, boolean showSetValues, int[] lineColors, int[] fillColors) {

        List<List<RadarEntry>> entriesList = new ArrayList<>();
        for (int i = 0; i < yXAxisValues.size(); ++i) {
            ArrayList<RadarEntry> entries = new ArrayList<>();
            for (int j = 0, n = yXAxisValues.get(i).size(); j < n; j++) {
                entries.add(new RadarEntry(yXAxisValues.get(i).get(j)));
            }
            entriesList.add(entries);
        }

        if (radarChart.getData() != null && radarChart.getData().getDataSetCount() > 0) {

            for (int i = 0; i < radarChart.getData().getDataSetCount(); ++i) {
                RadarDataSet set = (RadarDataSet) radarChart.getData().getDataSetByIndex(i);
                set.setValues(entriesList.get(i));
                set.setLabel(titles.get(i));
            }

            radarChart.getData().notifyDataChanged();
            radarChart.notifyDataSetChanged();
        } else {
            ArrayList<IRadarDataSet> dataSets = new ArrayList<>();

            for (int i = 0; i < entriesList.size(); ++i) {
                RadarDataSet set = new RadarDataSet(entriesList.get(i), titles.get(i));
                if (lineColors != null && fillColors != null) {
                    set.setColor(lineColors[i % entriesList.size()]);
                    set.setDrawFilled(true);
                    set.setFillAlpha(100);
                    set.setLineWidth(1f);
                    set.setDrawHighlightCircleEnabled(true);
                    set.setDrawHighlightIndicators(false);
                    set.setFillColor(fillColors[i % entriesList.size()]);
                } else {
                    set.setColor(LINE_COLORS[i % 3]);
                    set.setDrawFilled(true);
                    set.setFillAlpha(100);
                    set.setLineWidth(1f);
                    set.setDrawHighlightCircleEnabled(true);
                    set.setDrawHighlightIndicators(false);
                }

//                if (entriesList.size() == 1) {
//                    set.setDrawFilled(false);
//                    set.setFillColor(LINE_FILL_COLORS[i % 3]);
//                }
                dataSets.add(set);
            }

            RadarData data = new RadarData(dataSets);
            if (showSetValues) {
                data.setValueTextSize(6f);
                data.setValueFormatter(new IValueFormatter() {
                    @Override
                    public String getFormattedValue(float value, Entry entry, int i, ViewPortHandler viewPortHandler) {
                        return FormatUtils.format(value, 2);
                    }
                });
            } else {
                data.setDrawValues(false);
            }
            radarChart.setData(data);
            radarChart.invalidate();
        }
    }

    /**
     * 设置气泡图样式
     */
    public static void setBubbleChart(BubbleChart bubbleChart, final List<String> xAxisValue, List<List<Float>> yXAxisValues, List<String> titles, boolean showSetValues, int[] mColors) {

//        bubbleChart.setDrawGridBackground(false);
        bubbleChart.getDescription().setEnabled(false);
        bubbleChart.setTouchEnabled(true);

        bubbleChart.getDescription().setEnabled(false);

//        MPChartMarkerView markerView = new MPChartMarkerView(bubbleChart.getContext(), R.layout.custom_marker_view);
//        bubbleChart.setMarker(markerView);

        Matrix mMatrix = new Matrix();
        mMatrix.postScale((xAxisValue.size() > 6 ? (float) (Math.ceil(xAxisValue.size() / 6) * (xAxisValue.size() > 20 ? 1.3f : 1f)) : 0f), 0f);
        bubbleChart.getViewPortHandler().refresh(mMatrix, bubbleChart, true);

        //y坐标轴设置
        YAxis yAxis = bubbleChart.getAxisLeft();
        yAxis.setLabelCount(5, false);
        yAxis.setTextSize(X_TEXT_SIZE);
        yAxis.setDrawLabels(true);

        bubbleChart.getAxisRight().setEnabled(false);

        //x坐标轴设置
        XAxis xAxis = bubbleChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        xAxis.setGranularity(1f);
        xAxis.setLabelCount(xAxisValue.size());
        xAxis.setTextSize(X_TEXT_SIZE - 4);
        xAxis.setValueFormatter(new IAxisValueFormatter() {

            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return xAxisValue.get(((int) value) % xAxisValue.size());
            }
        });//设置自定义的x轴值格式化器
        xAxis.setEnabled(true);

        //图例设置
        Legend legend = bubbleChart.getLegend();
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);//图例水平居中
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);//图例在图表上方
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);//图例的方向为水平
        legend.setDrawInside(false);//绘制在chart的外侧
        legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);//图例中的文字方向

        legend.setForm(Legend.LegendForm.SQUARE);//图例窗体的形状
        legend.setFormSize(LEGEND_SIZE);//图例窗体的大小
        legend.setTextSize(LEGEND_TEXT_SIZE);//图例文字的大小

        //设置柱状图数据
        setBubbleChartData(bubbleChart, yXAxisValues, titles, showSetValues, mColors);

        bubbleChart.setExtraRightOffset(20f);
        bubbleChart.animateY(500);//数据显示动画，从左往右依次显示
    }


    /**
     * 设置气泡图数据
     */
    private static void setBubbleChartData(BubbleChart bubbleChart, List<List<Float>> yAxisValues, List<String> titles, boolean showSetValues, int[] lineColors) {

        List<List<BubbleEntry>> entriesList = new ArrayList<>();
        for (int i = 0; i < yAxisValues.size(); ++i) {
            ArrayList<BubbleEntry> entries = new ArrayList<>();
            for (int j = 0, n = yAxisValues.get(i).size(); j < n; j++) {
                entries.add(new BubbleEntry(j, yAxisValues.get(i).get(j), 10));
            }
            entriesList.add(entries);
        }

        if (bubbleChart.getData() != null && bubbleChart.getData().getDataSetCount() > 0) {

            for (int i = 0; i < bubbleChart.getData().getDataSetCount(); ++i) {
                BubbleDataSet set = (BubbleDataSet) bubbleChart.getData().getDataSetByIndex(i);
                set.setValues(entriesList.get(i));
                set.setLabel(titles.get(i));
            }

            bubbleChart.getData().notifyDataChanged();
            bubbleChart.notifyDataSetChanged();
        } else {
            ArrayList<IBubbleDataSet> dataSets = new ArrayList<>();

            for (int i = 0; i < entriesList.size(); ++i) {
                BubbleDataSet set = new BubbleDataSet(entriesList.get(i), titles.get(i));
                if (lineColors != null) {
                    set.setColor(lineColors[i % entriesList.size()]);
                    set.setDrawValues(true);
                } else {
                    set.setColor(LINE_COLORS[i % 3]);
                    set.setDrawValues(true);
                }
                dataSets.add(set);
            }

            BubbleData data = new BubbleData(dataSets);
            if (showSetValues) {
                data.setValueTextSize(10f);
                data.setValueFormatter(new IValueFormatter() {
                    @Override
                    public String getFormattedValue(float value, Entry entry, int i, ViewPortHandler viewPortHandler) {
                        return FormatUtils.format(value, 2) + "";
                    }
                });
            } else {
                data.setDrawValues(false);
            }

            data.setValueTextSize(8f);
            data.setValueTextColor(Color.WHITE);
            data.setHighlightCircleWidth(1.5f);

            bubbleChart.setData(data);
            bubbleChart.invalidate();
        }
    }


    /**
     * 设置散点图样式
     */
    public static void setScatterChart(ScatterChart scatterChart, final List<String> xAxisValue, List<List<Float>> yXAxisValues, List<String> titles, boolean showSetValues, int[] mColors) {

//        scatterChart.setDrawGridBackground(false);
        scatterChart.getDescription().setEnabled(false);
        scatterChart.setTouchEnabled(true);

        scatterChart.getDescription().setEnabled(false);
        if (!showSetValues) {
            MPChartMarkerView markerView = new MPChartMarkerView(scatterChart.getContext(), R.layout.custom_marker_view);
            scatterChart.setMarker(markerView);
        }

        Matrix mMatrix = new Matrix();
        mMatrix.postScale((xAxisValue.size() > 6 ? (float) (Math.ceil(xAxisValue.size() / 6) * (xAxisValue.size() > 20 ? 1.3f : 1f)) : 0f), 0f);
        scatterChart.getViewPortHandler().refresh(mMatrix, scatterChart, true);

        //y坐标轴设置
        YAxis yAxis = scatterChart.getAxisLeft();
        yAxis.setLabelCount(5, false);
        yAxis.setTextSize(X_TEXT_SIZE);
        yAxis.setDrawLabels(true);
        yAxis.setDrawGridLines(false);

        scatterChart.getAxisRight().setEnabled(false);

        //x坐标轴设置
        XAxis xAxis = scatterChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        xAxis.setGranularity(1f);//间距为1，影响x轴返回的value
        xAxis.setAxisMinimum(-1);
        xAxis.setAxisMaximum(xAxisValue.size());
        xAxis.setLabelCount(xAxisValue.size() + 2);
        xAxis.setTextSize(X_TEXT_SIZE - 4);
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                int position = (int) value;
                if (value < 0 || value >= xAxisValue.size() || position != value) {
                    return " ";
                } else {
                    return xAxisValue.get(position % xAxisValue.size());
                }
            }
        });//设置自定义的x轴值格式化器
        xAxis.setEnabled(true);
//        xAxis.setLabelRotationAngle(20);//倾斜角度
        //图例设置
        Legend legend = scatterChart.getLegend();
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);//图例水平左右中
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);//图例在图表下/上方
        legend.setOrientation(Legend.LegendOrientation.VERTICAL);//图例的方向
        legend.setDrawInside(false);//绘制在chart的外侧
        legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);//图例中的文字方向

        legend.setForm(Legend.LegendForm.SQUARE);//图例窗体的形状
        legend.setFormSize(LEGEND_SIZE);//图例窗体的大小
        legend.setTextSize(LEGEND_TEXT_SIZE);//图例文字的大小

        //设置柱状图数据
        setScatterChartData(scatterChart, yXAxisValues, titles, showSetValues, mColors);

        scatterChart.setFilterTouchesWhenObscured(true);
        scatterChart.animateY(500);//数据显示动画，从左往右依次显示

    }


    /**
     * 设置散点图数据
     */
    private static void setScatterChartData(ScatterChart scatterChart, List<List<Float>> yAxisValues, List<String> titles, boolean showSetValues, int[] lineColors) {

        List<List<Entry>> entriesList = new ArrayList<>();
        for (int i = 0; i < yAxisValues.size(); ++i) {
            ArrayList<Entry> entries = new ArrayList<>();
            for (int j = 0, n = yAxisValues.get(i).size(); j < n; j++) {
                entries.add(new Entry(j, yAxisValues.get(i).get(j)));
            }
            entriesList.add(entries);
        }

        if (scatterChart.getData() != null && scatterChart.getData().getDataSetCount() > 0) {

            for (int i = 0; i < scatterChart.getData().getDataSetCount(); ++i) {
                ScatterDataSet set = (ScatterDataSet) scatterChart.getData().getDataSetByIndex(i);
                set.setValues(entriesList.get(i));
                set.setLabel(titles.get(i));
            }

            scatterChart.getData().notifyDataChanged();
            scatterChart.notifyDataSetChanged();
        } else {
            ArrayList<IScatterDataSet> dataSets = new ArrayList<>();

            for (int i = 0; i < entriesList.size(); ++i) {
                ScatterDataSet set = new ScatterDataSet(entriesList.get(i), titles.get(i));
                if (lineColors != null) {
                    set.setColor(lineColors[i % entriesList.size()]);
                    set.setDrawValues(showSetValues);
                    set.setScatterShapeSize(40);
                    set.setScatterShape(ScatterChart.ScatterShape.CIRCLE);
                } else {
                    set.setColor(LINE_COLORS[i % 3]);
                    set.setDrawValues(showSetValues);
                    set.setScatterShapeSize(40);
                    set.setScatterShape(ScatterChart.ScatterShape.CIRCLE);
                }
                dataSets.add(set);
            }

            ScatterData data = new ScatterData(dataSets);
            if (showSetValues) {
                data.setValueTextSize(10f);
                data.setValueFormatter(new IValueFormatter() {
                    @Override
                    public String getFormattedValue(float value, Entry entry, int i, ViewPortHandler viewPortHandler) {
                        return FormatUtils.format(value, 2) + "%";
                    }
                });
            } else {
                data.setDrawValues(false);
            }

            data.setValueTextSize(8f);
            data.setValueTextColor(Color.parseColor("#4e4e4e"));

            scatterChart.setData(data);
            scatterChart.invalidate();
        }
    }
}
